<?php

class UserController extends Zend_Controller_Action {

    protected $_authNamespace;
    protected $_redirector = null;

    public function init() {
        /* Initialize action controller here */
        $this->_authNamespace = new Zend_Session_Namespace('Zend_Auth');
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }

    public function indexAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $user = new Application_Model_UserMapper();
            $this->view->entries = $user->fetchAll();
        } else {
            return $this->_helper->redirector->gotoUrl('/auth/login');
        }
    }

    public function addUserAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $user_id = $this->_getParam("user_id");
            $request = $this->getRequest();
            $form = new Application_Form_User();
            $form_data = array("user" => $user_id);
            $form->populate($form_data);

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    // first check for errors...

                    $user = new  Application_Model_User();
                    $mapper  = new Application_Model_UserMapper();
                    $user = $mapper->findByUsername($form->getValue('username'));

                    $exist = true;
                    if ($user == null) {
                        $exist = false;
                    }

                    if ($form->getValue('password') == '') {
                        $this->view->message = "The password field cannot be blank.";
                    } else if ($exist) {
                        $this->view->message = "This username already exists.";
                    } else {
                        $user = new Application_Model_User($form->getValues());
                        $user->setUsername($form->getValue('username'));
                        $user->setPassword($form->getValue('password'));
                        $user->setRealName($form->getValue('real_name'));
                        $user->setRole($form->getValue('role'));
                        $user->setEmail($form->getValue('email'));


                        $mapper = new Application_Model_UserMapper();
                        $mapper->save($user);

                        $this->_helper->redirector('index');
                    }
                }
            }

            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function editUserAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $user_id = $this->_getParam("user_id");
            $request = $this->getRequest();
            $form = new Application_Form_User();
            $this->view->user_id = $user_id;

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array("id" => (int) $user_id);

                    $user = new Application_Model_User($data);

                    $mapper = new Application_Model_UserMapper();
                    $mapper->find($user_id, $user);

                    //$user->setUsername($form->getValue('username'));
                    $user->setPassword($form->getValue('password'));
                    $user->setRealName($form->getValue('real_name'));
                    $user->setRole($form->getValue('role'));
                    $user->setEmail($form->getValue('email'));
                    
                    $mapper->save($user);
                    
                    $this->_helper->redirector('index');
                }
            } else {
                $data = array("id" => (int) $user_id);

                $user = new Application_Model_User($data);

                $mapper = new Application_Model_UserMapper();
                $mapper->find($user_id, $user);
                $form_data = array("id" => $user->getId(), "username" => $user->getUsername(), "password" => $user->getPassword(), "real_name" => $user->getRealName(), "role" => $user->getRole(), "email" => $user->getEmail());
                $form->populate($form_data);
            }


            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function deleteAction() {
        if ($this->_authNamespace->userRole == 'admin') {
            $user_id = $this->_getParam("user_id");
            $this->view->idValue = $user_id;
            if ($user_id != null) {
                $data = array("id" => (int) $user_id);

                $user = new Application_Model_User($data);

                $mapper = new Application_Model_UserMapper();

                $u = new Application_Model_User();
                $mapper->find((int) $user_id, $u);

                $mapper->delete($user);

                $this->_helper->redirector('index');
            }
        } else {
            $this->view->message = "You don't have permissions to do this!";
            return $this->_helper->redirector('index');
        }
    }

}

